package com.mytx.mapper;


import com.mytx.pojo.User;
import com.mytx.pojo.UserExample;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface UserMapper {

    /**
     * 增加用户保证金
     *
     * @param uId
     * @param amount
     * @return 保证金
     */
    @Select(" UPDATE user  SET deposit = deposit + #{amount} WHERE u_id = #{uId};SELECT deposit FROM user WHERE u_id = #{uId}")
    BigDecimal incrementDeposit(@Param("uId") String uId, @Param("amount") BigDecimal amount);

    /**
     * 增加用户余额
     *
     * @param uid
     * @param amount
     */
    @Select(" UPDATE user  SET balance = balance + #{amount} WHERE u_id = #{uId};SELECT balance FROM user WHERE u_id = #{uId}")
    BigDecimal incrementBalance(@Param("uId") String uid, @Param("amount") BigDecimal amount);

    /**
     * 减少用户余额,减少保证金金额，增加捐赠金额，捐赠是自动生成的，捐赠金额是整个支出的一部分
     *
     * @param uId
     * @param amount
     */
    @Select(" UPDATE user  SET balance = balance - #{amount},deposit = deposit - #{amount},donation_amount =  donation_amount + #{donationAmount},updated= #{updated} WHERE u_id = #{uId};SELECT balance ,deposit,donation_amount as donationAmount FROM user WHERE u_id = #{uId}")
    User payOfferV1(@Param("uId") String uId, @Param("amount") BigDecimal amount, @Param("donationAmount") BigDecimal donationAmount,@Param("updated")Date updated);

    /**
     * 支付 ：减少用户账户余额，减少保证金 金额，减少的账户余额  等于  减少的保证金金额
     *
     * @param uId
     * @param amount
     */
    @Select(" UPDATE user  SET balance = balance - #{amount},deposit = deposit - #{amount} WHERE u_id = #{uId};SELECT balance ,deposit FROM user WHERE u_id = #{uId}")
    User pay(@Param("uId") String uId, @Param("amount") BigDecimal amount);

    /**
     * 支付 ：减少用户账户余额，减少保证金 金额，减少的账户余额
     *
     * @param uId     用户id
     * @param amount  减少的账户余额
     * @param deposit 减少的保证金 金额
     * @return 返回用户账户余额和保证金金额
     */
    @Select(" UPDATE user  SET balance = balance - #{amount},deposit = deposit - #{deposit} WHERE u_id = #{uId};SELECT balance ,deposit FROM user WHERE u_id = #{uId}")
    User payV2(@Param("uId") String uId, @Param("amount") BigDecimal amount, @Param("deposit") BigDecimal deposit);


    /**
     * 减少用户押金金额
     *
     * @param uId
     * @param amount
     */
    @Select(" UPDATE user  SET deposit =  deposit - #{amount} WHERE u_id = #{uId};SELECT deposit FROM user WHERE u_id = #{uId}")
    BigDecimal reduceDeposit(@Param("uId") String uId, @Param("amount") BigDecimal amount);

    /**
     * 减少用户余额
     *
     * @param uId
     * @param amount
     */
    @Select(" UPDATE user  SET balance =  balance - #{amount} WHERE u_id = #{uId};SELECT balance FROM user WHERE u_id = #{uId}")
    BigDecimal reduceBalance(@Param("uId") String uId, @Param("amount") BigDecimal amount);

    /**
     * 增加用户余额，并减少用户捐赠金额
     * 用于拆回捐赠的时候
     *
     * @return
     */
    @Select(" UPDATE user  SET balance =  balance + #{amount},donation_amount = donation_amount- #{amount}    WHERE u_id = #{uId};SELECT balance ,donation_amount as donationAmount FROM user WHERE u_id = #{uId}")
    User incrementBalanceAndReduceDonationAmount(@Param("uId") String uId, @Param("amount") BigDecimal amount);

    /**
     * 提现
     * 减少捐赠总额，减少钱包余额
     *
     * @param amount
     * @param uId
     */

    @Select(" UPDATE user  SET deposit =  deposit - #{amount},balance = balance - #{amount} WHERE u_id = #{uId}; SELECT balance,deposit  FROM user WHERE u_id = #{uId}")
    User reduceDonationAmount(@Param("amount") BigDecimal amount, @Param("uId") String uId);

    /**
     * 捐赠
     * 增加捐赠总额，减少钱包余额
     *
     * @param amount
     * @param uId
     */

    @Select(" UPDATE user  SET donation_amount =  donation_amount + #{amount},balance = balance - #{amount},updated= #{updated} WHERE u_id = #{uId}; SELECT balance,donation_amount as donationAmount FROM user WHERE u_id = #{uId}")
    User incrementDonationAmount(@Param("amount") BigDecimal amount, @Param("uId") String uId,@Param("updated")Date updated);


    /**
     * 收到捐赠
     * 增加钱包余额，增加收到的捐赠总额
     * 多个参数，需要使用 @Param
     *
     * @param increment
     * @param uId
     */
    @Select(" UPDATE user  SET balance = @value:= balance + #{increment},donation_received =@value:= donation_received + #{increment},updated= #{updated}  WHERE u_id = #{uId}; SELECT balance ,donation_received as donationReceived FROM user WHERE u_id = #{uId}")
    User incrementReceivedDonationAmount(@Param("increment") BigDecimal increment, @Param("uId") String uId,@Param("updated")Date updated);

    /**
     * 自己增加的方法，通过id查询个人用户信息，
     * 后续需要增加查询的字段，去mapper.xml增加
     * 实体类中必须要有对应字段的构造函数 否则报错
     *
     * @param uId
     * @return
     */
    @Select("select u_id as uId,gender,area,donation_amount,avatar,username from user  where u_id = #{uId}")
    User selectByUid(String uId);

    @Select("select donation_amount from user  where u_id = #{uId}")
    Double selectDonationAmount(String uId);

    List<User> selectUserByIds(@Param(value = "list") List<String> userIdList);

    //----------------------------------------------------分割线------------------------------------------------
    int countByExample(UserExample example);

    int deleteByExample(UserExample example);

    int deleteByPrimaryKey(String uId);

    int insert(User record);

    int insertSelective(User record);

    List<User> selectByExample(UserExample example);

    User selectByPrimaryKey(String uId);


    int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);

    int updateByExample(@Param("record") User record, @Param("example") UserExample example);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);

}